
// 提示框
function modelShow(
    title = '提示',
    content = '确认执行此操作吗?',
    callback = () => { },
    showCancel = true,
    cancelText = '取消',
    confirmText = '确定'
) {
    uni.showModal({
        title: title,
        content: content,
        showCancel: showCancel,
        cancelText: cancelText,
        confirmText: confirmText,
        cancelText: cancelText,
        success: function (res) {
            if (res.confirm) {
                // 用户点击确定操作
                setTimeout(() => {
                    callback()
                }, 500)
            } else if (res.cancel) {
                // 用户取消操作
            }
        }
    })
};


//货币格式化
function formatMoney(number, places, symbol, thousand, decimal) {
    number = number || 0
    places = !isNaN((places = Math.abs(places))) ? places : 2
    symbol = symbol !== undefined ? symbol : '￥'
    thousand = thousand || ','
    decimal = decimal || '.'
    var negative = number < 0 ? '-' : '',
        i = parseInt((number = Math.abs(+number || 0).toFixed(places)), 10) + '',
        j = (j = i.length) > 3 ? j % 3 : 0
    return (
        symbol +
        negative +
        (j ? i.substr(0, j) + thousand : '') +
        i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + thousand) +
        (places ?
            decimal +
            Math.abs(number - i)
                .toFixed(places)
                .slice(2) :
            '')
    )
}

/**
 * 金额相加
 * @param {Object} value1
 * @param {Object} value2
 */
function moneySum(value1, value2) {
    return (parseFloat(value1) + parseFloat(value2)).toFixed(2);
}
/**
 * 金额相减
 * @param {Object} value1
 * @param {Object} value2
 */
function moneySub(value1, value2) {
    let res = (parseFloat(value1) - parseFloat(value2)).toFixed(2);
    return res > 0 ? res : 0;
}

/**
 * 时间格式
 * @param {Object} time
 * @param {Object} type(ymd，ymd-hms，hm)
 */
function longDateFormate(time, type) {
	if(!time) return '';
  
  const timestamp = new Date(time).getTime();
  if(timestamp<0){
    return ''
  }
	
  time = time.replace(/T/g, ' '); //去掉T
  time = time.replace(/-/g, '/');
  time = time.replace(/\.\d+/, ' ');//去掉毫秒
  
	const timeFlag = new Date(time);
  
	let y = timeFlag.getFullYear();
	let m = numFormat(timeFlag.getMonth() + 1);
	let d = numFormat(timeFlag.getDate());
	let h = numFormat(timeFlag.getHours());
	let min = numFormat(timeFlag.getMinutes());
	let s = numFormat(timeFlag.getSeconds());
	if (type == 'ymd') {
	    return `${y}-${m}-${d}`;
	} 
  if(type=='ymd-hms'){
		return `${y}-${m}-${d} ${h}:${min}:${s}`;
	}
  if(type=='hm'){
		return `${h}:${min}`;
	}
  
	return `${y}-${m}-${d} ${h}:${min}`;
}

/**
 * string转int
 * @param {Object} d 
 */
const numFormat = (d = '') => {
    const data = d * 1;
    return data < 10 ? '0' + data : data;
};

/**
 * 根据id获取返回的label
 * @param {Object} id
 * @param {Object} list
 */
function getLabelFromID(id,list = []) {
	const info = list.find(item => { return id == item.value; });
	if (info) {
		return info.label;
	}
	return '';
}

function getTextFromID (id = '', list = [] , name = 'text'){
    const info = list.find(item => { return id == item.id; });
    if (info) {
        return list.find(item => { return id == item.id; })[name];
    }
    return '';
};


export {
    formatMoney,
    modelShow,
    moneySum,
    moneySub,
	longDateFormate,
	getLabelFromID,
	getTextFromID,
}
